initUsers();
function initUsers() {
    $.ajax({
        url: '/employee.do',
        method: 'GET',
        success: function (result) {
            if (result && result.length > 0) {
                for (var i = 0; i < result.length; i++) {
                    var val = result[i];
                    if (val.state != 1) { continue; }
                     $(`<tr>
                 <td><img src="../img/${val.photo}" /></td>
                 <td>${val.id}</td>
                 <td>${val.name}</td>
                 <td>${val.phone}</td>
                 <td>${val.sex}</td>
                 <td>${val.age}</td>
                 <td>${val.idCard}</td>
                 <td>${val.role}</td>
                 <td>
                 <a myId="${val.id}" class="am-btn am-btn-success xiugai" href="add.html?id=${val.id}">修改</a>
                 <button myId="${val.id}" class="am-btn am-btn-danger shanchu">删除</button>
                 </td >
                 </tr > `).appendTo('tbody');
                }
            }
        }

    })
}
//删除员工
$(document).on('click', ".shanchu", function () {
    let id = $(this).attr('myId');
    swal({
        title: '确定删除吗？',
        text: '你将无法恢复它！',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: '确定删除！',
        cancelButtonText: '取消',

    }).then(function (result) {
        $.ajax({
            url: '/del.do',
            method: 'post',
            data: `id=${id}`,
            success: function (result) {

                if (result == 'success') {
                    swal('删除成功', '用户以被删除', 'success');
                    location.reload();
                } else {
                    swal('删除失败', '用户未被删除', 'error');
                }
            }
        });

        //swal(JSON.stringify(result))
    }).catch(swal.noop)
})

//修改用户
// $(document).on('click', ".xiugai", function () {
//     let id = $(this).attr('myId');
//     swal({
//         title: '修改用户',
//         html:
//             '<input id="name" class="swal2-input" placeholder="请输入用户名" autocomplete="off">' +
//             '<input id="phone" class="swal2-input" placeholder="请输入用户手机号" autocomplete="off">' +
//             '<input id="idCard" class="swal2-input" placeholder="请输入用户身份证号" autocomplete="off">' +
//             '<input id="age" class="swal2-input" placeholder="请输入用户年龄" autocomplete="off">'+
//             '<select id="role"><option>经理<option><option>财务<option><option>保安<option><option>保洁<option><select>'+
//             '<select id="sex"><option>男<option><option>女<option></select>',
//         preConfirm: function () {
//             return new Promise(function (resolve) {
//                 resolve([
//                     $('#name').val(),
//                     $('#phone').val(),
//                     $('#idCard').val(),
//                     $('#age').val(),
//                     $('#role').val(),
//                     $('#sex').val(),
//                 ])
//             })
//         },
//         onOpen: function () {
//             $('#name').focus()
//         }
//     }).then(function (result) {

//         if (!result) { return; }
//         //Ajax
//         $.ajax({
//             url: '/del_xiugai.do',
//             method: 'POST',
//             data: `id=${id}&name=${$('#name').val()}&phone=${$('#phone').val()}&idCard=${$('#idCard').val()}&age=${$('#age').val()}&role=${$('#role').val()}&sex=${$('#sex').val()}`,
//             success: function (result) {
//                 if (result === 'success') {
//                     location.reload()
//                 } else {
//                     swal('修改失败', '密码重置失败', 'error');
//                 }
//             }
//         })

// }).catch(swal.noop)
// })
/**
 * 修改用户
 * */
getParams();
function getParams(){
    //从URL参数中获取员工ID
    let id = location.search.split('=')[1];
    if(!id){
        $("#myTitle").html('添加员工信息');
    }else{
        $("#myTitle").html('修改员工信息');
        $.get('/queryEmployeebyld.do',{id},function(employee){
            //数据回填
            $('#name').val(employee.name);
            $('#age').val(employee.age);
            $('#phone').val(employee.phone);
            $('#idCard').val(employee.idCard);
            $('#role_select').val(employee.role);
            $('input[value="+employee.sex+"]').prop('checked',true);
            $('#myphoto').attr('src','/img/'+employee.photo);
            $('#myphoto').siblings('figcaption').hidi;
        })
    }
}